//
//  ShellHomeUserView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/3.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellHomeUserView.h"
#import "ShellHomeViewModel.h"

@interface  ShellHomeUserView()

@property (nonatomic,strong) ShellHomeViewModel *viewModel;

//信用
@property (nonatomic,strong) UILabel *creditNumLabel;

//算力
@property (nonatomic,strong) UILabel *calculateNumLabel;
@property (nonatomic,strong) UILabel *addCalculateLab;
@property (nonatomic,strong) UIView *addCalculateView;




@end

@implementation ShellHomeUserView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.viewModel = (ShellHomeViewModel *)viewModel;
        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    CGFloat btnW = (K_SCREENWIDTH - 30 - 10)/2.0;
    
    ///!!!:信用
    UIButton *creditBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:creditBtn];
    [creditBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(70);
    }];
    creditBtn.backgroundColor = UIColorHex(0x08233F);
    creditBtn.cornerRadius = 6;
   
    UILabel *creditLab = [UILabel lz_labelWithText:LZLocalizedString(@"我的信用") fontSize:k_textFont(12) color:UIColorHex(0x156097)];
    [creditLab lz_centerAlignment];
    [creditBtn addSubview:creditLab];
    [creditLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(17);
    }];
    
    [creditBtn addSubview:self.creditNumLabel];
    [self.creditNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(creditLab.mas_bottom).mas_offset(7);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(25);
    }];
    
    
    
    ///!!!:算力
    UIButton *calculateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:calculateBtn];
    [calculateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(70);
    }];
    calculateBtn.backgroundColor = UIColorHex(0x08233F);
    calculateBtn.cornerRadius = 6;
    UILabel *calculateLab = [UILabel lz_labelWithText:LZLocalizedString(@"我的算力") fontSize:k_textFont(12) color:UIColorHex(0x156097)];
    [calculateLab lz_centerAlignment];
    [calculateBtn addSubview:calculateLab];
    [calculateLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(17);
    }];
    
    [calculateBtn addSubview:self.calculateNumLabel];
    [self.calculateNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(calculateLab.mas_bottom).mas_offset(7);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(btnW);
        make.height.mas_equalTo(25);
    }];
    
    
      [calculateBtn addSubview:self.addCalculateView];
      [self.addCalculateView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.centerY.mas_equalTo(self.calculateNumLabel.mas_centerY);
          make.left.mas_equalTo(self.calculateNumLabel.mas_right).mas_offset(6);
          make.width.mas_equalTo(50);
          make.height.mas_equalTo(18);
      }];
    self.addCalculateView.cornerRadius = 9;
    [self.addCalculateView setGradientBackgroundWithColors:@[UIColorHex(0x8EE7F5),UIColorHex(0x45B6FF)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(0.51, 0.01) endPoint:CGPointMake(0.51, 0.99)];
   
    UIImageView *imageView = [[UIImageView alloc] initWithImage:k_imageName(@"icon_shandian")];
    [self.addCalculateView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.addCalculateView.mas_centerY);
        make.left.mas_equalTo(6);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(13);
    }];
    
     [self.addCalculateView addSubview:self.addCalculateLab];
     [self.addCalculateLab mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.mas_equalTo(self.addCalculateView.mas_centerY);
         make.left.mas_equalTo(imageView.mas_right).mas_offset(3);
         make.width.mas_equalTo(30);
         make.height.mas_equalTo(18);
     }];
    
    
    
    
    
    
    
    self.creditNumLabel.text = @"0";
    self.calculateNumLabel.text = @"0";

    
    
    [[creditBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(self.viewModel.creditScoreDesc.length == 0)return ;
         [LZToolView showAlertType:LZAlertViewTypTipDetail withTitle:LZLocalizedString(@"信用分说明") message:self.viewModel.creditScoreDesc cancel:@"" sure:@"" objectDict:nil cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {}];
    }];

    [[calculateBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(self.viewModel.hashRateDesc.length == 0)return ;
        [LZToolView showAlertType:LZAlertViewTypTipDetail withTitle:LZLocalizedString(@"算力说明") message:self.viewModel.hashRateDesc cancel:@"" sure:@"" objectDict:nil cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {}];
    }];
    
    
    //翻译
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        creditLab.text = LZLocalizedString(@"我的信用");
        calculateLab.text = LZLocalizedString(@"我的算力");

    }];
    
    @weakify(self);
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        
         self.creditNumLabel.text = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.creditScore].saveDecimal;
         self.calculateNumLabel.text = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.hashRate].saveDecimal;
        
        CGFloat calculateTotal = (K_SCREENWIDTH - 30 - 10)/2.0;
        if(self.viewModel.accountModel.level > 4){
            NSString *allHashRate = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.allHashRate].saveDecimal;
            self.addCalculateLab.text = [NSString stringWithFormat:@"+%@",allHashRate];
            self.addCalculateView.hidden = NO;
            CGFloat calculateNumWid = [self.calculateNumLabel.text lz_textWidthWithFontSize:self.calculateNumLabel.font withMaxHeight:20];
            CGFloat addCalculateNumWid = [self.addCalculateLab.text lz_textWidthWithFontSize:self.addCalculateLab.font withMaxHeight:20];
            CGFloat x = (calculateTotal - calculateNumWid - 25 - addCalculateNumWid) /2.0;
            
            [self.calculateNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(x);
                make.width.mas_equalTo(calculateNumWid);
            }];
            
            [self.calculateNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(x);
                make.width.mas_equalTo(calculateNumWid);
            }];
            
            [self.addCalculateLab mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(addCalculateNumWid);
            }];
            
            [self.addCalculateView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(addCalculateNumWid + 25);
            }];
            
            
        } else {
            self.addCalculateView.hidden = YES;

            [self.calculateNumLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.width.mas_equalTo(calculateTotal);
            }];
            
        }
        
        
        
        
        
        
    }];
    
    
}

//信用
- (UILabel *)creditNumLabel{
    if(!_creditNumLabel){
        _creditNumLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(18) color:UIColorHex(0xFFFFFF)];
        [_creditNumLabel lz_centerAlignment];
        
    }
    return _creditNumLabel;
}

//信用
- (UILabel *)calculateNumLabel{
    if(!_calculateNumLabel){
        _calculateNumLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(18) color:UIColorHex(0xFFFFFF)];
        [_calculateNumLabel lz_centerAlignment];

    }
    return _calculateNumLabel;
}


//增加算力
- (UILabel *)addCalculateLab{
    if(!_addCalculateLab){
        _addCalculateLab = [UILabel lz_labelWithText:@"" fontSize:k_textFont(12) color:UIColorHex(0xFFFFFF)];
    }
    return _addCalculateLab;
}


- (UIView *)addCalculateView{
    if (!_addCalculateView) {
        _addCalculateView = [[UIView alloc] init];
        _addCalculateView.hidden = YES;
    }
    return _addCalculateView;
}

@end
